/*
 * Copyright (c) 2007 Marco Glietsch (http://www.mikrocontroller.net/topic/98697)
 * Original author: Marco Glietsch
 * Modified for octopus by Michael Hartmann <ich@speicherleck.de>
 * All rights reserved.
 *
 * Short descripton of file:
 *
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright 
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above 
 *     copyright notice, this list of conditions and the following 
 *     disclaimer in the documentation and/or other materials provided 
 *     with the distribution.
 *   * Neither the name of the FH Augsburg nor the names of its 
 *     contributors may be used to endorse or promote products derived 
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES{} LOSS OF USE, 
 * DATA, OR PROFITS{} OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef CAN_H
#define CAN_H

#include <inttypes.h>

#define setbit(ADR,BIT)   (ADR |= (1<<BIT))
#define clearbit(ADR,BIT) (ADR &=~(1<<BIT))
#define getbit(ADR, BIT)  (ADR &  (1<<BIT))

/* CAN Objekte */
#define NO_MOBS 15   // Anzahl der Message Objects festlegen
#define NOMOB   0xff // Definition für ungültigen Objektindex

// baudrate
enum { CAN_BAUDRATE_100K, CAN_BAUDRATE_125K, CAN_BAUDRATE_200K,
       CAN_BAUDRATE_250K, CAN_BAUDRATE_500K, CAN_BAUDRATE_1000K };

// Betriebsmodi
enum { CAN_MODE_DISABLED, CAN_MODE_TRANSMIT_DATA, CAN_MODE_TRANSMIT_REMOTE,
       CAN_MODE_RECEIVE_DATA, CAN_MODE_AUTO_REPLY };

// Interrupt-Modi
enum { CAN_INTERRUPT_NONE, CAN_INTERRUPT_TX, CAN_INTERRUPT_RX,
       CAN_INTERRUPT_TXRX };

void can_parser(uint8_t *buf);

int can_init(uint8_t baudrate, uint8_t intmode, uint8_t eid);
void can_init_usb(uint8_t baudrate, uint8_t eid);

int can_deinit(void);
void can_deinit_usb(void);

int can_enable_mob(uint8_t mob, uint8_t mode, uint32_t id, uint32_t idm);
void can_enable_mob_usb(uint8_t mob, uint8_t mode, uint32_t id, uint32_t idm);

int can_disable_mob(uint8_t mob);
void can_disable_mob_usb(uint8_t mob);

int can_send_data(uint8_t mob, uint32_t id, uint8_t length, uint8_t *data);
void can_send_data_usb(uint8_t mob, uint32_t id, uint8_t length, uint8_t *data);

int can_send_remote(uint8_t mob);
void can_send_remote_usb(uint8_t mob);

void can_receive_data_usb(int mob);

void can_set_autoreply(uint8_t mob, uint8_t length, uint8_t *buf);
void can_set_autoreply_usb(uint8_t mob, uint8_t length, uint8_t *buf);

uint8_t can_silent_mode(uint8_t mode);
void can_silent_mode_usb(uint8_t mode);

// sonsitge hilfsfunktionen
int can_set_baudrate(uint8_t baudrate);
int can_set_interrupt(uint8_t mode);
void can_select_mob(uint8_t mob);
uint32_t can_get_id(void);
void can_set_id_mask(uint32_t idm);
void can_set_id(uint32_t id);
int can_set_mode(uint8_t mode);
uint8_t can_get_mode(void);
void can_set_data(uint8_t length, uint8_t * data);
void can_get_data(uint8_t *msg);
void can_set_mob_interrupt(uint8_t object);
void can_clear_mob_interrupt(uint8_t mob);
uint8_t can_get_mob_interrupt(void);
void can_get_mob(uint8_t mob);

#endif /* CAN_H */
